package org.dragonnova.lamp.utils;

import java.nio.ByteBuffer;

/**
 * Created by nick on 2016/4/23.
 * Disccription:数据处理的Util
 */
public class DataUtil {
    public static int byteToUnsignedByte(byte b) {
        int result = b & 0xFF;
        return result;
    }

    /**
     * byte[] 转换为 int
     * byte[] 从低位到高位排列
     */
    public static int bytesToIntLittleEndian(byte... bytes) {
        if (null == bytes || bytes.length == 0) {
            return 0;
        }
       /* if (!(bytes.length == 4 || bytes.length == 2)) {
            throw new IllegalArgumentException("invalid bytes'len");
        }*/

        int len = bytes.length - 1;
        int result = 0;
        for (int i = len; i >= 0; i--) {
            int b = byteToUnsignedByte(bytes[i]);
            result += b << 8 * i;
        }
        return result;
    }

    /**
     * int 转换为数组
     * byte[] 从高位到低位排列
     */
    public static byte[] int2byteBigEndian(int res) {
        byte[] targets = new byte[4];
        targets[3] = (byte) (res & 0xff);// 最低位
        targets[2] = (byte) ((res >> 8) & 0xff);// 次低位
        targets[1] = (byte) ((res >> 16) & 0xff);// 次高位
        targets[0] = (byte) (res >>> 24);// 最高位,无符号右移。
        return targets;
    }

    /**
     * int 转换为数组
     * byte[] 从低位到高位排列
     */
    public static byte[] int2byteLittleEndian(int res) {
        byte[] targets = new byte[4];
        targets[0] = (byte) (res & 0xff);// 最低位
        targets[1] = (byte) ((res >> 8) & 0xff);// 次低位
        targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
        targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。
        return targets;
    }

    /**
     * int 转换为程度为 2 的 byte[]数组
     * byte[] 从低位到高位排列
     */
    public static byte[] int2byteLen2Low(int res) {
        byte[] bytes = int2byteLittleEndian(res);
        return new byte[]{bytes[0], bytes[1]};
    }

    /**
     * int 转换为程度为 3 的 byte[]数组
     * byte[] 从低位到高位排列
     */
    public static byte[] int2byteLen3Low(int res) {
        byte[] bytes = int2byteLittleEndian(res);
        return new byte[]{bytes[0], bytes[1], bytes[2]};
    }

    /**
     * byte 转换为 long
     * byte[] 从低位到高位排列
     */
    public static long bytes2Long(byte[] res) {
        long num = 0;
        for (int ix = res.length - 1; ix >= 0; ix--) {
            num <<= 8;
            num |= (res[ix] & 0xff);
        }
        return num;
    }

    /**
     * byte 转换为 long 的第二种实现
     * byte[] 从低位到高位排列
     */
    public static long bytes2Long2(byte[] res) {
        ByteBuffer buffer = ByteBuffer.allocate(8);
        for (int i = 0; i < 8; i++) {
            int pos = 8 - i - 1;
            byte val = (pos < res.length) ? res[pos] : 0;
            buffer.put(val);
        }
        buffer.flip();
        long val = buffer.getLong();
        return val;
    }

    /**
     * long 转换为 byte[] ,取低三位
     * byte[] 从低位到高位排列
     */
    public static byte[] long2Bytes3Low(long res) {

        int newInt = 0;
        if (res > 1000000) {
            char[] chars = String.valueOf(res).toCharArray();
            String newString = String.valueOf(res).substring(chars.length - 7, chars.length);
            newInt = Integer.parseInt(newString);
        } else {
            newInt = Long.valueOf(res).intValue();
        }
        return DataUtil.int2byteLen3Low(newInt);
    }

    /**
     * long 转换为 byte[] ,取低四位
     * byte[] 从低位到高位排列
     */
    public static byte[] long2Bytes4Low(long res) {

        int newInt = 0;
        if (res > 1000000000) {
            char[] chars = String.valueOf(res).toCharArray();
            String newString = String.valueOf(res).substring(chars.length - 9, chars.length);
            newInt = Integer.parseInt(newString);
        } else {
            newInt = Long.valueOf(res).intValue();
        }
        return DataUtil.int2byteLittleEndian(newInt);
    }


    /**
     * unsigned char to byte
     * char 数据 转换为 byte[] ,先将 char 转换为 int，然后取最低位字节
     */
    public static byte[] chars2Bytes(char[] res) {
        byte[] bytes = new byte[res.length];
        for (int i = 0; i < res.length; i++) {
            bytes[i] = (byte) (res[i]);
        }
        return bytes;
    }

    /**
     * String to unsigned char
     * String 数据 转换为 byte[] ,先将 String 转为 char 然后将 char 转换为 int，然后取最低位字节
     */
    public static byte[] string2Bytes(String res) {
        char[] chars = res.toCharArray();
        return chars2Bytes(chars);
    }

    /**
     * unsigned char to string
     * byte 数据装换为int 然后转换为char，最后拼接
     */
    public static String bytes2String(byte[] res) {
        StringBuilder builder = new StringBuilder();
        char[] charResult = new char[res.length];
        for (int i = 0; i < res.length; i++) {
            int temp = byteToUnsignedByte(res[i]);
            charResult[i] = (char) temp;
        }
        return builder.append(charResult).toString();
    }
}
